#include "DL_Message_Handler.h"
#include <rtthread.h>         //Operate的同步用到了OS的事件机制

 struct rt_event Tcyc_OS_Event; //Tcyc的事件对象

#define DL_MH_MaxRetry 2U
//Message handler的状态数据
 DL_MessageHandler_STATE_TYPE DL_MH_CurrentState[PORT_NUM];
//发送Msquence重试的次数
uint8_t DL_MH_Retry[PORT_NUM];
 
 
//MC序列特性，每次调用DL_SetMode服务时候更新
MC_Struct_TypeDef MC_Struct[PORT_NUM];
 
 uint16_t MasterCycleTime[PORT_NUM];//单位为0.1ms 当前使用的运行周期CycleTime，主站依据从站的minCycleTime.
 uint16_t Tbit[PORT_NUM]; //单个位的传输时间，在刚建立通信的时候确定。
 

 //SM_DL_STATE_TYPE CMD_IN_OPERATE[PORT_NUM];// 暂时保存在Operate状态时候接收到的模式转换命令，用于DL_SetMode服务

/**********************辅助函数**********************/
//根据MC_Struct的读写方向和MC类型，决定发送和接收长度（接收地址）
void MY_Update_MC_Struct2(uint8_t Port){
    	 
    switch (MC_Struct[Port].M_sequenceType)
    {
    case TYPE_0:{
         
         if (MC_Struct[Port].Dirction==Direction_Read){
             MC_Struct[Port].RecevieBuffer=MC_Struct[Port].MC;  //Max14819接收时候，会多接受一个中断字和RxBytesAct
             *MC_Struct[Port].TxBytes=2;
             *MC_Struct[Port].RxBytes=2;
					    MC_Struct[Port].SPI_Tx_Bytes=5;
					   MC_Struct[Port].SPI_Rx_Bytes=4;
             
         }else{
             MC_Struct[Port].RecevieBuffer=MC_Struct[Port].MC+1;             
             *MC_Struct[Port].TxBytes=3;
             *MC_Struct[Port].RxBytes=1;
					    MC_Struct[Port].SPI_Tx_Bytes=6;
					    MC_Struct[Port].SPI_Rx_Bytes=3;
         }
        break;
    }
    case TYPE_1_1_And_TYPE_1_2:{
         
        if(MC_Struct[Port].Dirction==Direction_Read){
             
            MC_Struct[Port].RecevieBuffer=MC_Struct[Port].MC;      //Max14819接收时候，会多接受一个中断字和RxBytesAct      
           * MC_Struct[Port].TxBytes=2;
            *MC_Struct[Port].RxBytes=3;
					  MC_Struct[Port].SPI_Tx_Bytes=5;
					   MC_Struct[Port].SPI_Rx_Bytes=5;
        }else{
            
            MC_Struct[Port].RecevieBuffer=MC_Struct[Port].MC+2;       //Max14819接收时候，会多接受一个中断字和RxBytesAct    
            *MC_Struct[Port].TxBytes=4;
            *MC_Struct[Port].RxBytes=1;
					  MC_Struct[Port].SPI_Tx_Bytes=7;
					 MC_Struct[Port].SPI_Rx_Bytes=3;
        }
       break; 
    }
 
    case TYPE_1_V:{
       
        if(MC_Struct[Port].Dirction==Direction_Read){
            MC_Struct[Port].RecevieBuffer=MC_Struct[Port].MC;        //Max14819接收时候，会多接受一个中断字和RxBytesAct         
            *MC_Struct[Port].TxBytes=2;
            *MC_Struct[Port].RxBytes=MC_Struct[Port].ODLength+1;
					   MC_Struct[Port].SPI_Tx_Bytes=*MC_Struct[Port].TxBytes+3;
					   MC_Struct[Port].SPI_Rx_Bytes=*MC_Struct[Port].RxBytes+2;
        }else{
             
            MC_Struct[Port].RecevieBuffer=MC_Struct[Port].MC+MC_Struct[Port].ODLength;
            *MC_Struct[Port].TxBytes=MC_Struct[Port].ODLength+2;
             *MC_Struct[Port].RxBytes=1;
					    MC_Struct[Port].SPI_Tx_Bytes=*MC_Struct[Port].TxBytes+3;
					   MC_Struct[Port].SPI_Rx_Bytes=*MC_Struct[Port].RxBytes+2;
        }
        break;
    }

    case TYPE_2_1:{
    
        if(MC_Struct[Port].Dirction==Direction_Read){
            MC_Struct[Port].RecevieBuffer=MC_Struct[Port].MC;
            *MC_Struct[Port].TxBytes=2;
            *MC_Struct[Port].RxBytes=3;
					 	    MC_Struct[Port].SPI_Tx_Bytes=*MC_Struct[Port].TxBytes+3;
					   MC_Struct[Port].SPI_Rx_Bytes=*MC_Struct[Port].RxBytes+2;
					 
        }else{
            MC_Struct[Port].RecevieBuffer=MC_Struct[Port].MC+1;
            *MC_Struct[Port].TxBytes=3;
            *MC_Struct[Port].RxBytes=2;
					  MC_Struct[Port].SPI_Tx_Bytes=*MC_Struct[Port].TxBytes+3;
					   MC_Struct[Port].SPI_Rx_Bytes=*MC_Struct[Port].RxBytes+2;
        }
        break;
    }
    case TYPE_2_2:{
       
         if(MC_Struct[Port].Dirction==Direction_Read){
            MC_Struct[Port].RecevieBuffer=MC_Struct[Port].MC;
            *MC_Struct[Port].TxBytes=2;
            *MC_Struct[Port].RxBytes=4;
					    MC_Struct[Port].SPI_Tx_Bytes=*MC_Struct[Port].TxBytes+3;
					   MC_Struct[Port].SPI_Rx_Bytes=*MC_Struct[Port].RxBytes+2;
        }else{
            MC_Struct[Port].RecevieBuffer=MC_Struct[Port].MC+1;
            *MC_Struct[Port].TxBytes=3;
            *MC_Struct[Port].RxBytes=3;
					     MC_Struct[Port].SPI_Tx_Bytes=*MC_Struct[Port].TxBytes+3;
					   MC_Struct[Port].SPI_Rx_Bytes=*MC_Struct[Port].RxBytes+2;
        }
        break;
    }
    case TYPE_2_3:{
      
         if(MC_Struct[Port].Dirction==Direction_Read){
            MC_Struct[Port].RecevieBuffer=MC_Struct[Port].MC+1;
            *MC_Struct[Port].TxBytes=3;
            *MC_Struct[Port].RxBytes=2;
					       MC_Struct[Port].SPI_Tx_Bytes=*MC_Struct[Port].TxBytes+3;
					   MC_Struct[Port].SPI_Rx_Bytes=*MC_Struct[Port].RxBytes+2;
        }else{
            MC_Struct[Port].RecevieBuffer=MC_Struct[Port].MC+2;
           * MC_Struct[Port].TxBytes=4;
            *MC_Struct[Port].RxBytes=1;
					     MC_Struct[Port].SPI_Tx_Bytes=*MC_Struct[Port].TxBytes+3;
					   MC_Struct[Port].SPI_Rx_Bytes=*MC_Struct[Port].RxBytes+2;
        }
        break;
    }   
    case TYPE_2_4:{
        
         if(MC_Struct[Port].Dirction==Direction_Read){
            MC_Struct[Port].RecevieBuffer=MC_Struct[Port].MC+2;
            *MC_Struct[Port].TxBytes=4;
            *MC_Struct[Port].RxBytes=2;
					        MC_Struct[Port].SPI_Tx_Bytes=*MC_Struct[Port].TxBytes+3;
					   MC_Struct[Port].SPI_Rx_Bytes=*MC_Struct[Port].RxBytes+2;
        }else{
            MC_Struct[Port].RecevieBuffer=MC_Struct[Port].MC+3;
            *MC_Struct[Port].TxBytes=5;
            *MC_Struct[Port].RxBytes=1;
					      MC_Struct[Port].SPI_Tx_Bytes=*MC_Struct[Port].TxBytes+3;
					   MC_Struct[Port].SPI_Rx_Bytes=*MC_Struct[Port].RxBytes+2;
        }
        break;
    }

    case TYPE_2_5:{
     
         if(MC_Struct[Port].Dirction==Direction_Read){
            MC_Struct[Port].RecevieBuffer=MC_Struct[Port].MC+1;
            *MC_Struct[Port].TxBytes=3;
            *MC_Struct[Port].RxBytes=3;
					       MC_Struct[Port].SPI_Tx_Bytes=*MC_Struct[Port].TxBytes+3;
					   MC_Struct[Port].SPI_Rx_Bytes=*MC_Struct[Port].RxBytes+2;
        }else{
            MC_Struct[Port].RecevieBuffer=MC_Struct[Port].MC+2;
            *MC_Struct[Port].TxBytes=4;
            *MC_Struct[Port].RxBytes=2;
					  MC_Struct[Port].SPI_Tx_Bytes=*MC_Struct[Port].TxBytes+3;
					   MC_Struct[Port].SPI_Rx_Bytes=*MC_Struct[Port].RxBytes+2;
        }
        break;
    }
    case TYPE_2_V:{
         if(MC_Struct[Port].Dirction==Direction_Read){
            MC_Struct[Port].RecevieBuffer=MC_Struct[Port].MC+MC_Struct[Port].PDOutputLength;
            *MC_Struct[Port].TxBytes=2+MC_Struct[Port].PDOutputLength;
            *MC_Struct[Port].RxBytes=MC_Struct[Port].ODLength+MC_Struct[Port].PDInputLength+1;
					  MC_Struct[Port].SPI_Tx_Bytes=*MC_Struct[Port].TxBytes+3;
					   MC_Struct[Port].SPI_Rx_Bytes=*MC_Struct[Port].RxBytes+2;
        }else{
            MC_Struct[Port].RecevieBuffer=MC_Struct[Port].MC+MC_Struct[Port].PDOutputLength+MC_Struct[Port].ODLength;
            *MC_Struct[Port].TxBytes=2+MC_Struct[Port].PDOutputLength+MC_Struct[Port].ODLength;
            *MC_Struct[Port].RxBytes=MC_Struct[Port].PDInputLength+1;
					  MC_Struct[Port].SPI_Tx_Bytes=*MC_Struct[Port].TxBytes+3;
					   MC_Struct[Port].SPI_Rx_Bytes=*MC_Struct[Port].RxBytes+2;
        }
        break;
    }

    default:
        break;
    }
        
}




//MH初始化
void DL_MH_Init(void){
   for(uint8_t i=0;i<PORT_NUM;i++){
        DL_MH_CurrentState[i]=Inactive_0;
        DL_MH_Retry[i]=0;
		     
        
        MC_Struct[i].CKT=&BasicBufferInOut[i][4];
		    MC_Struct[i].MC=&BasicBufferInOut[i][3];
		    MC_Struct[i].TxBytes=&BasicBufferInOut[i][2];
		    MC_Struct[i].RxBytes=&BasicBufferInOut[i][1];		    
        MC_Struct[i].SendBuffer=&BasicBufferInOut[i][0];
		    //初始化MC的一些初值 ,TYPE0
       MC_Struct[i].M_sequenceType=TYPE_0;
      MC_Struct[i].M_squenceTime=0;
	    MC_Struct[i].ODLength=1;
	    MC_Struct[i].PDInputLength=0;
	    MC_Struct[i].PDOutputLength=0;
		 
		 //初始化Operate模式下的模式转换命令暂存区
	//	 CMD_IN_OPERATE[i]=COMLOST;
		 
		  //初始化MasterCycleTime
		   MasterCycleTime[i]=0;
        
   }
	  rt_err_t result;

    /* 初始化Tcyc事件对象 */
    result = rt_event_init(&Tcyc_OS_Event, "Tcyc", RT_IPC_FLAG_PRIO);
    if (result != RT_EOK)
    {
        rt_kprintf("init event failed.\n");
         
    }

}
//初始化单个端口
void DL_MH_InitPort(uint8_t i){
	
	        DL_MH_CurrentState[i]=Inactive_0;
        DL_MH_Retry[i]=0;
		     
        
        MC_Struct[i].CKT=&BasicBufferInOut[i][4];
		    MC_Struct[i].MC=&BasicBufferInOut[i][3];
		    MC_Struct[i].TxBytes=&BasicBufferInOut[i][2];
		    MC_Struct[i].RxBytes=&BasicBufferInOut[i][1];		    
        MC_Struct[i].SendBuffer=&BasicBufferInOut[i][0];
		    //初始化MC的一些初值 ,TYPE0
       MC_Struct[i].M_sequenceType=TYPE_0;
      MC_Struct[i].M_squenceTime=0;
	    MC_Struct[i].ODLength=1;
	    MC_Struct[i].PDInputLength=0;
	    MC_Struct[i].PDOutputLength=0;
		 
		 //初始化Operate模式下的模式转换命令暂存区
		 CMD_IN_OPERATE[i]=COMLOST;
	
}

/**********************输出Event******************/
//自定义子事件，用于触发ISDU状态机从自定义子状态返回
//当MC接收到返回值时候触发
void Event_OD_Return(uint8_t Port){
    if(ISDU_STATE[Port]==subISDUWaitPage_1_1 ){  
       
       ISDU_STATE[Port]=ISDU_Idle_1;
			 DL_ISDU_T13_1(Port); 
       return;
    }
		
    if(ISDU_STATE[Port]==subISDUWaitReturnISDU_3_1 ){
       
       ISDU_STATE[Port]=ISDUWait_3;
			 DL_ISDU_T5_1(Port);
       return;
    }
    if(ISDU_STATE[Port]==subISDUWaitReturnISUD_5_1){
       
       ISDU_STATE[Port]=ISDUResponse_5;
			DL_ISDU_T7_1(Port);
       return;
    }
    if(EH_STATE[Port]==subEH_Idle_1){
       EH_STATE[Port]=EH_ReadEvent_2;
       DL_EH_T2_1(Port);
       return;
    }
    if(EH_STATE[Port]==subEH_ReadEvent_2){
       EH_STATE[Port]=EH_ReadEvent_2;
       DL_EH_T3_1(Port);
    }
    if(EH_STATE[Port]==subEH_EventConfirmation_4){
       EH_STATE[Port]=EH_Idle_1;
       DL_EH_T8(Port);
    }

   
}


//自定义子事件，用于触发ISDU和EH状态机从自定义子状态返回
//当MC接收出错时候触发  
/************************************* 此函数无必要，因为Mode状态机会负责这些工作
void Event_OD_Error_Return(uint8_t Port){
    if(ISDU_STATE[Port]==subISDUWaitPage_1_1 ){  
      // DL_ISDU_T13_1(Port); 
       ISDU_STATE[Port]=ISDU_Idle_1;
       return;
    }
    if(ISDU_STATE[Port]==subISDUWaitReturnISDU_3_1 ){
      // DL_ISDU_T5_1(Port);
       ISDU_STATE[Port]=ISDUWait_3;
       return;
    }
    if(ISDU_STATE[Port]==subISDUWaitReturnISUD_5_1){
      // DL_ISDU_T7_1(Port);
       ISDU_STATE[Port]=ISDUResponse_5;
       return;
    }
    if(EH_STATE[Port]==subEH_Idle_1){
       EH_STATE[Port]=EH_ReadEvent_2;
       //DL_EH_T2_1(Port);
       return;
    }
    if(EH_STATE[Port]==subEH_ReadEvent_2){
       EH_STATE[Port]=EH_ReadEvent_2;
      // DL_EH_T3_1(Port);
    }
    if(EH_STATE[Port]==subEH_EventConfirmation_4){
       EH_STATE[Port]=EH_Idle_1;
     //  DL_EH_T8_1(Port);
    }

   
}

****************************/

/******************进入函数 Enter******


void Enter_Operate_12(uint8_t Port){
	  	 //判断 是否有命令切换状态
	
	 if(CMD_IN_OPERATE[Port]==COMLOST){ //没有切换命令
		    return;		 
	 }
	 else if(CMD_IN_OPERATE[Port]==STARTUP&&CH_STATE[Port]==CH_Idle_1){ //有切换命令，且已经通知了从站
		         CMD_IN_OPERATE[Port]=COMLOST;
		         Event_DL_SetMode_STARTUP(Port);  
						     //等状态改变后 改变MC结构
			   	  MC_Struct_INIT(Port);
		        return;
	 }else if(CMD_IN_OPERATE[Port]==INACATIVE&&CH_STATE[Port]==CH_Idle_1){
		          CMD_IN_OPERATE[Port]=COMLOST;
		           Event_DL_SetMode_INACTIVE(Port);
						     //等状态改变后 改变MC结构
						  MC_Struct_INIT(Port);
							return;    
		 
	 }else{
		      return;
	 }
	
}
*************/
/*********************Message_Handler状态机的转移动作******************/
void DL_MH_T1(uint8_t Port){
   
  // Send_MC(Port); 如果用MAX硬件。不需要这个
}

void DL_MH_T2(uint8_t Port){
	//此处读取Max14819相应寄存器，返回 MinCycleTime
	//待更改
}

void DL_MH_T5(uint8_t Port){
   //MY_Update_MC_Struct2(Port); send_MC中已经有调用
   DL_MH_Retry[Port]=0;
   Send_MC(Port);
}

void DL_MH_T6(uint8_t Port){
   // MY_Update_MC_Struct2(Port); send_MC中已经有调用
  DL_MH_Retry[Port]=0;
  Send_MC(Port);
}

void DL_MH_T7(uint8_t Port){
   if(DL_MH_Retry[Port]==DL_MH_MaxRetry){
      Event_Retry_equel_MaxRetry(Port);
   }else{
      Event_tm_Tinitcyc(Port);
   }
}

void DL_MH_T8(uint8_t Port){
   if(DL_MH_Retry[Port]==DL_MH_MaxRetry){
      Event_Retry_equel_MaxRetry(Port);
   }else{
      Event_tm_Tinitcyc(Port);
   }
}

void DL_MH_T9(uint8_t Port){
   DL_MH_Retry[Port]++;
   //重新发送
   Send_MC(Port);
}

void DL_MH_T10(uint8_t Port){
    
    
			PAGE_BufferIn[Port]=*MC_Struct[Port].OD; //获取OD数据，存入Page IN通道
    
}

void DL_MH_T11(uint8_t Port){
   Event_MHInfo_COMLOST(Port);
   //Event_DL_Page_Cnf(Port);
   

}

void DL_MH_T13(uint8_t Port){
   Event_ODTrig(Port);
}
void DL_MH_T14(uint8_t Port){
   Event_ODTrig(Port);
}
void DL_MH_T15(uint8_t Port){
   Event_ODTrig(Port);
}
void DL_MH_T16(uint8_t Port){
   Event_ODTrig(Port);
}
void DL_MH_T17(uint8_t Port){
   Event_ODTrig(Port);
}

void DL_MH_T18(uint8_t Port){
   DL_MH_Retry[Port]=0;
   MY_Update_MC_Struct2(Port);
   Send_MC(Port);
}

void DL_MH_T19(uint8_t Port){
   if(DL_MH_Retry[Port]>=DL_MH_MaxRetry){
      Event_Retry_equel_MaxRetry(Port);
   }else{
      Event_tm_Tinitcyc(Port);
   }
}

void DL_MH_T20(uint8_t Port){
    if(DL_MH_Retry[Port]>=DL_MH_MaxRetry){
      Event_Retry_equel_MaxRetry(Port);
   }else{
      Event_tm_Tinitcyc(Port);
   }
}

void DL_MH_T21(uint8_t Port){
   DL_MH_Retry[Port]++;
   Send_MC(Port);
}

void DL_MH_T22(uint8_t Port){
   Event_MHInfo_COMLOST(Port);
   //Event_OD_Error_Return(Port);//自定义事件，作用于ISDU EH，使之从sub状态回到正常状态
}
    
void DL_MH_T24(uint8_t Port){
	 DL_MH_CurrentState[Port]=GetOD_7;
   Event_ODTrig(Port);
}

void DL_MH_T25(uint8_t Port){
   DL_MH_CurrentState[Port]=Preoperate_6;
}

void DL_MH_T23(uint8_t Port){
   //啥都不做？
   Event_OD_Return(Port); //通知ISDU 有返回值
	
	 if(EH_STATE[Port]!=EH_Idle_1||ISDU_STATE[Port]!=ISDU_Idle_1){
	 
		 
		      DL_MH_T24(Port);
		      return;
	 }else  {
		       DL_MH_T25(Port);
	 }

 
}

void DL_MH_T26_39(uint8_t Port){
   StartCycleTimer(Port,MasterCycleTime[Port]); //开启软定时器
	 //开启Operate线程
	  
	  
}


void DL_MH_T27(uint8_t Port){
   //发送PDTrig
   Event_PDTrig(Port);
   
}

void DL_MH_T28(uint8_t Port){
   //发送ODTrig
   Event_ODTrig(Port);
}

void DL_MH_T29(uint8_t Port){
   DL_MH_Retry[Port]=0;
  // MY_Update_MC_Struct2(Port);
   Send_MC(Port);
}
 
void DL_MH_T30(uint8_t Port){
   if(DL_MH_Retry[Port]>=DL_MH_MaxRetry){
      Event_Retry_equel_MaxRetry(Port);
   }else{
      Event_tm_Tinitcyc(Port);
   }
}

void DL_MH_T31(uint8_t Port){
   if(DL_MH_Retry[Port]>=DL_MH_MaxRetry){
      Event_Retry_equel_MaxRetry(Port);
   }else{
      Event_tm_Tinitcyc(Port);
   }
}


void DL_MH_T32(uint8_t Port){
   DL_MH_Retry[Port]++;
   Send_MC(Port);
}



void DL_MH_T33(uint8_t Port){
	StopCycleTimer(Port); //停止Operate定时器，
   Event_MHInfo_COMLOST(Port);
   //Event_OD_Error_Return(Port);//自定义事件，作用于ISDU EH，使之从sub状态回到正常状态
}


void DL_MH_T34(uint8_t Port){
   //通知ISDU，MC传送结束,主要用于驱动其子状态回归
   Event_OD_Return(Port); //通知ISDU提取OD内容，相当于OD.Cnf
   //PD.cnf 从MC获取PDin内容
   Event_PD_Return(Port); //通知PD提取PD内容，相当于PD.Cnf
	 
   //Enter_Operate_12(Port);//进入Operate状态所执行的工作
		 
		 

}


void DL_MH_T35(uint8_t Port){
 
	   Event_MHInfo_COMLOST(Port);
	 StopCycleTimer(Port);//从Operate返回到其他状态需要停止周期定时器
	 
}
 
void DL_MH_T36(uint8_t Port){
	    Event_MHInfo_COMLOST(Port);
	   
}




void DL_MH_T38(uint8_t Port){
	   
	   StopCycleTimer(Port);//从Operate返回到其他状态需要停止周期定时器
	
}
/*******************输入Event**********************/

void Event_MH_Conf_COMx(uint8_t Port){
    if(DL_MH_CurrentState[Port]==Inactive_0){
       
       DL_MH_CurrentState[Port]=AwaitReply_1;
			 DL_MH_T1(Port); 
       return;  
    }
}


void Event_DL_Write(uint8_t Port){
   if(DL_MH_CurrentState[Port]==MH_Startup_2){
      
      DL_MH_CurrentState[Port]=AwaitReply_4;
      
      DL_MH_T6(Port);
   }
}

void Event_DL_Read(uint8_t Port){
   if(DL_MH_CurrentState[Port]==MH_Startup_2){
      
      DL_MH_CurrentState[Port]=AwaitReply_4;
      
      DL_MH_T5(Port);
   }
}


void Event_DL_ReadParam(uint8_t Port){
   if(DL_MH_CurrentState[Port]==Preoperate_6){
      
      DL_MH_CurrentState[Port]=GetOD_7;
		  DL_MH_T13(Port);

   }

}

void Event_DL_WriteParam(uint8_t Port){
   if(DL_MH_CurrentState[Port]==Preoperate_6){
      
      DL_MH_CurrentState[Port]=GetOD_7;
		  DL_MH_T14(Port);

   }
}

void Event_DL_ISDUTransport(uint8_t Port){
   if(DL_MH_CurrentState[Port]==Preoperate_6){
     
      DL_MH_CurrentState[Port]=GetOD_7; 
      DL_MH_T15(Port);
   }
}

void Event_EventFlag(uint8_t Port){ //此函数还改变OD——Handler状态
   if(DL_MH_CurrentState[Port]==Preoperate_6){
      DL_OD_State[Port]=Event_3; //同时更改OD状态
      DL_MH_CurrentState[Port]=GetOD_7;
      DL_MH_T16(Port);
   }
}

void Event_MH_DL_Write_DEVICEMODE(uint8_t Port){
 
		 
	 //通知MH触发ODTrig
   if(DL_MH_CurrentState[Port]==Preoperate_6){
      
      DL_MH_CurrentState[Port]=GetOD_7;
      DL_MH_T17(Port);
   }
}

void Event_OD_Tinitcyc(uint8_t Port){
   if(DL_MH_CurrentState[Port]==GetOD_7){
      
      DL_MH_CurrentState[Port]=AwaitReply_9;
      DL_MH_T18(Port);
   }

}

 





 

void Event_MH_Conf_INACTIVE(uint8_t Port){
      if(DL_MH_CurrentState[Port]==Operate_12||DL_MH_CurrentState[Port]==AwaitReply_16){
				    
				    DL_MH_CurrentState[Port]=Inactive_0;
	         DL_MH_InitPort(Port); //初始化每个端口
				  DL_MH_T35(Port);
			}
			else if(DL_MH_CurrentState[Port]==Preoperate_6||DL_MH_CurrentState[Port]==AwaitReply_9){
				    DL_MH_CurrentState[Port]=Inactive_0;
	         DL_MH_InitPort(Port); //初始化每个端口
				   DL_MH_T36(Port);
				
			}
      
 
}



void Event_MH_Conf_PREOPERATE(uint8_t Port){
   if(DL_MH_CurrentState[Port]==MH_Startup_2||DL_MH_CurrentState[Port]==AwaitReply_4){
      //DL_MH_T12(Port); 
      DL_MH_CurrentState[Port]=Preoperate_6;
		  
   }
  
}

 
void Event_MH_Conf_Startup(uint8_t Port){
   if(DL_MH_CurrentState[Port]==Preoperate_6||DL_MH_CurrentState[Port]==AwaitReply_9){
      //DL_MH_T37(Port);
      DL_MH_CurrentState[Port]=MH_Startup_2;
   }
   else if(DL_MH_CurrentState[Port]==Operate_12||DL_MH_CurrentState[Port]==AwaitReply_16){
     
      DL_MH_CurrentState[Port]=MH_Startup_2;
		  DL_MH_T38(Port);

   }
}

 
void Event_MH_Conf_OPERATE(uint8_t Port){
   if(DL_MH_CurrentState[Port]==Preoperate_6||DL_MH_CurrentState[Port]==AwaitReply_9){
      
      DL_MH_CurrentState[Port]=Operate_12;
      DL_MH_T26_39(Port);
   }
   else if(DL_MH_CurrentState[Port]==MH_Startup_2||DL_MH_CurrentState[Port]==AwaitReply_4){
     // DL_MH_T39(Port);
      DL_MH_CurrentState[Port]=Operate_12;
		  DL_MH_T26_39(Port);
   }

}



 

//此事件对于Max14819不存在，Max14819内部有MCquenceTimer和CycleTimer。使用Event_Response_not_OK替代
//void Event_tm_TM_sequence(uint8_t Port){
//    if(DL_MH_CurrentState[Port]==AwaitReply_4){
//      
//       DL_MH_CurrentState[Port]=ErrorHandling_5;
//        DL_MH_T7(Port);
//       return;
//    }
//    else if(DL_MH_CurrentState[Port]==AwaitReply_9){
//          DL_MH_CurrentState[Port]=Error_Handling_10;
//          DL_MH_T19(Port);
//    }
//    else if(DL_MH_CurrentState[Port]==AwaitReply_16){
//         DL_MH_CurrentState[Port]=ErrorHandling_17;
//         DL_MH_T30(Port);
//    }
//    

//}


//由硬件Max14819产生的Error中断调用
//Max14819内部有CycleTimer会周期性的从FIFO发送数据，所以在MH中省去了ErrorHandling状态和tmCycle事件

void Event_Response_not_OK(uint8_t Port){
	
	 //当初次建立通信失败调用这里
   if(DL_MH_CurrentState[Port]==AwaitReply_1){
     // DL_MH_T3(Port);
      DL_MH_CurrentState[Port]=Inactive_0;
      return;
   }  
	  //当发送数据失败调用
    if(DL_MH_CurrentState[Port]==AwaitReply_4){
     
    // DL_MH_CurrentState[Port]=ErrorHandling_5; 如果用Max14819无需此状态
     DL_MH_Retry[Port]++;
     if(DL_MH_Retry[Port]>=2){
         Event_Retry_equel_MaxRetry(Port);
     }else{
         Send_MC(Port);
     }
     
     return;
   }
    if(DL_MH_CurrentState[Port]==AwaitReply_9){
     //  DL_MH_CurrentState[Port]=Error_Handling_10;  如果用Max14819无需此状态
       DL_MH_Retry[Port]++;
      if(DL_MH_Retry[Port]>=2){
         Event_Retry_equel_MaxRetry(Port);
     }else{
         Send_MC(Port);
     }
      return;
   }
   if(DL_MH_CurrentState[Port]==AwaitReply_16){
     //DL_MH_CurrentState[Port]=ErrorHandling_17;  如果用Max14819无需此状态
       DL_MH_Retry[Port]++;
      if(DL_MH_Retry[Port]>=2){
         Event_Retry_equel_MaxRetry(Port);
     }else{
         Send_MC(Port);
     }
     return;
   }
}


//Max14819中也不需要此函数，因为其内部有MCsquenceTimer
 void Event_tm_Tinitcyc(uint8_t Port){
//   if(DL_MH_CurrentState[Port]==ErrorHandling_5&&DL_MH_Retry[Port]<DL_MH_MaxRetry){
//     
//     DL_MH_CurrentState[Port]=AwaitReply_4;
//		 DL_MH_T9(Port);
//   }
//   else if(DL_MH_CurrentState[Port]==Error_Handling_10&&DL_MH_Retry[Port]<DL_MH_MaxRetry){
//     
//     DL_MH_CurrentState[Port]=AwaitReply_9;
//		 DL_MH_T21(Port);
//   }
 }



//此事件并非由CycleTimer启动，而是由T34启动，Max14819会使用内部Cycle定时器，定时的将数据从FIFO中发出去
void Event_Tcyc(uint8_t Port){
     if(DL_MH_CurrentState[Port]==Operate_12){
			    DL_MH_CurrentState[Port]=GetPD_13;
          DL_MH_T27(Port);
		 }
      
		   
   
}

void Event_PD(uint8_t Port){
   if(DL_MH_CurrentState[Port]==GetPD_13){
      DL_MH_CurrentState[Port]=GetOD_14;
      DL_MH_T28(Port);
      
   }
}

void Event_OD(uint8_t Port){
   if(DL_MH_CurrentState[Port]==GetOD_14){
       
      DL_MH_CurrentState[Port]=AwaitReply_16;
      DL_MH_T29(Port);
      return;
   }
   if(DL_MH_CurrentState[Port]==GetOD_7){
      DL_MH_CurrentState[Port]=AwaitReply_9;
      DL_MH_T18(Port);
   }
}



void Event_Response_OK(uint8_t Port){
   if(DL_MH_CurrentState[Port]==AwaitReply_1){
     
     DL_MH_CurrentState[Port]=MH_Startup_2;
		 DL_MH_T2(Port);
     return;
   }
   
}

void Event_No_error(uint8_t Port){
   if(DL_MH_CurrentState[Port]==AwaitReply_4){  //当状态转换时，需要通知从站，从而需要调用Page通信，此时主站可能处于AwaitReply状态就开始转换了
      
      DL_MH_CurrentState[Port]=MH_Startup_2;
		  DL_MH_T10(Port);
      return;
   }
   if(DL_MH_CurrentState[Port]==AwaitReply_9){
      
      DL_MH_CurrentState[Port]=CheckHandler_11;
		 DL_MH_T23(Port);
      return;
   }
   if(DL_MH_CurrentState[Port]==AwaitReply_16){
      
      DL_MH_CurrentState[Port]=Operate_12;
      DL_MH_T34(Port);
      return;
   }
}

/******************采用查看ISDU等状态机状态判断
void Event_Not_idle(uint8_t Port){
   if(DL_MH_CurrentState[Port]==CheckHandler_11){
      DL_MH_T24(Port);
      DL_MH_CurrentState[Port]=GetOD_7;
   }
}

void Event_Idle(uint8_t Port){
   if(DL_MH_CurrentState[Port]==CheckHandler_11){
      DL_MH_T25(Port);
      DL_MH_CurrentState[Port]=Preoperate_6;
   }
}
**************************************/



void Event_Retry_equel_MaxRetry(uint8_t Port){
	 //给MH通信解锁
	// DL_MH_Param[Port].MH_LOCK=UNLOCK;
    if(DL_MH_CurrentState[Port]==AwaitReply_4){
     
      DL_MH_CurrentState[Port]=Inactive_0;
			 DL_MH_T11(Port);
      return;
    }
    if(DL_MH_CurrentState[Port]==AwaitReply_9){
       
       DL_MH_CurrentState[Port]=Inactive_0;
			DL_MH_T22(Port);
       return;
    }
    else if(DL_MH_CurrentState[Port]==AwaitReply_16){
     
      DL_MH_CurrentState[Port]=Inactive_0;
			 DL_MH_T33(Port);
      return;
    }
		
		DL_MH_InitPort(Port);

}


 

